package com.jxsa.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.http.*;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;

/**
 * @author pwq
 */
@RestController
public class OrderController {

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/order")
    public String order(){
        Map<String,Object> details = (Map<String,Object>)(((OAuth2Authentication) SecurityContextHolder.getContext()
                .getAuthentication()).getUserAuthentication().getDetails());
        return JSON.toJSONString(details.get("principal"));
    }

    @GetMapping("/user/info")
    public String userInfo(){
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON,new MediaType("Authorization","bearer 8ff1b5b2013d4f2abf18f1eb2b24a212")));
        HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
        ResponseEntity<String> result = restTemplate.exchange("http://jxsa-auth/user/info", HttpMethod.GET, entity, String.class);
        return result.toString();
    }
}
